#include "dockermanager.h"

#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>

typedef QList<QJsonObject> JsonObjectList;

DockerManager::DockerManager(QObject *parent)
    : QObject{parent}
{

}

DockerImageList DockerManager::images()
{
    DockerImageList list;
    JsonObjectList objects = docker2Json("images");

    while (!objects.isEmpty()) {
        QJsonObject obj = objects.takeFirst();
        list << obj;
    }

    return list;
}

DockerPsList DockerManager::ps(bool all)
{
    DockerPsList list;
    JsonObjectList objects;
    if (all) {
        objects = docker2Json("ps", "-a", "--no-trunc");
    } else {
        objects = docker2Json("ps", "--no-trunc");
    }

    while (!objects.isEmpty()) {
        QJsonObject obj = objects.takeFirst();
        list << obj;
    }

    return list;
}

DockerSearchList DockerManager::search(const QString &input)
{
    DockerSearchList list;
    JsonObjectList objects = docker2Json("search", input, "--no-trunc");

    while (!objects.isEmpty()) {
        QJsonObject obj = objects.takeFirst();
        list << obj;
    }

    m_docker_search_result = list;
    return list;
}

DockerInspect DockerManager::inspect(const QString &name)
{
    JsonObjectList objects = docker2Json("inspect", name);

    if (!objects.isEmpty()) {
        return DockerInspect(objects.takeFirst());
    }

    return DockerInspect(QJsonObject());
}

QList<QString> DockerManager::docker(QStringList arguments)
{
    QList<QString> lines;

    QProcess process;
    process.start("docker", arguments);
    process.waitForFinished();
    while (process.canReadLine()) {
        QString line = process.readLine();
        lines.append(line);
    }

    return lines;
}

QList<QJsonObject> DockerManager::docker2Json(QStringList arguments)
{
    arguments << "--format" << "{{json .}}";

    QList<QJsonObject> objects;

    QList<QString> lines = docker(arguments);
    while (!lines.isEmpty()) {
        QString line = lines.takeFirst();
        QJsonDocument doc = QJsonDocument::fromJson(line.toUtf8());
        if (!doc.isObject()) {
            continue;
        }
        QJsonObject obj = doc.object();
        objects.append(obj);
    }

    return objects;
}

QList<QJsonObject> DockerManager::docker2Json(QString arg1)
{
    QStringList arguments;
    arguments << arg1;
    return docker2Json(arguments);
}

QList<QJsonObject> DockerManager::docker2Json(QString arg1, QString arg2)
{
    QStringList arguments;
    arguments << arg1 << arg2;
    return docker2Json(arguments);
}

QList<QJsonObject> DockerManager::docker2Json(QString arg1, QString arg2, QString arg3)
{
    QStringList arguments;
    arguments << arg1 << arg2 << arg3;
    return docker2Json(arguments);
}

QList<QJsonObject> DockerManager::RunDocker(QStringList arguments)
{
    QList<QJsonObject> objects;

    QProcess process;
    process.start("docker", arguments);
    process.waitForFinished();
    while (process.canReadLine()) {
        QString line = process.readLine();
        QJsonDocument doc = QJsonDocument::fromJson(line.toUtf8());
        if (!doc.isObject()) {
            continue;
        }

        QJsonObject obj = doc.object();
        objects.append(obj);
    }

    return objects;
}

DockerSearchList DockerManager::docker_search_result() const
{
    return m_docker_search_result;
}
